﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
namespace Gobang
{
    public class PokerCard : PictureBox
    {
        public static List<Bitmap> cardImages;
        public readonly static double RATIO = 1.5094;
        public static int CARD_W = 150;
        public static int CARD_H = (int)(RATIO * CARD_W);
        public static int DELTA_Y = 40;
        
        public int number;
        public int color;
        public bool bChosen = false;
        static PokerCard()
        {
            cardImages = new List<Bitmap>();
            cardImages.Add(Properties.Resources._1diamond);
            cardImages.Add(Properties.Resources._1club);
            cardImages.Add(Properties.Resources._1heart);
            cardImages.Add(Properties.Resources._1spade);
            cardImages.Add(Properties.Resources._2diamond);
            cardImages.Add(Properties.Resources._2club);
            cardImages.Add(Properties.Resources._2heart);
            cardImages.Add(Properties.Resources._2spade);
            cardImages.Add(Properties.Resources._3diamond);
            cardImages.Add(Properties.Resources._3club);
            cardImages.Add(Properties.Resources._3heart);
            cardImages.Add(Properties.Resources._3spade);
            cardImages.Add(Properties.Resources._4diamond);
            cardImages.Add(Properties.Resources._4club);
            cardImages.Add(Properties.Resources._4heart);
            cardImages.Add(Properties.Resources._4spade);
            cardImages.Add(Properties.Resources._5diamond);
            cardImages.Add(Properties.Resources._5club);
            cardImages.Add(Properties.Resources._5heart);
            cardImages.Add(Properties.Resources._5spade);
            cardImages.Add(Properties.Resources._6diamond);
            cardImages.Add(Properties.Resources._6club);
            cardImages.Add(Properties.Resources._6heart);
            cardImages.Add(Properties.Resources._6spade);
            cardImages.Add(Properties.Resources._7diamond);
            cardImages.Add(Properties.Resources._7club);
            cardImages.Add(Properties.Resources._7heart);
            cardImages.Add(Properties.Resources._7spade);
            cardImages.Add(Properties.Resources._8diamond);
            cardImages.Add(Properties.Resources._8club);
            cardImages.Add(Properties.Resources._8heart);
            cardImages.Add(Properties.Resources._8spade);
            cardImages.Add(Properties.Resources._9diamond);
            cardImages.Add(Properties.Resources._9club);
            cardImages.Add(Properties.Resources._9heart);
            cardImages.Add(Properties.Resources._9spade);
            cardImages.Add(Properties.Resources._10diamond);
            cardImages.Add(Properties.Resources._10club);
            cardImages.Add(Properties.Resources._10heart);
            cardImages.Add(Properties.Resources._10spade);
            cardImages.Add(Properties.Resources._11diamond);
            cardImages.Add(Properties.Resources._11club);
            cardImages.Add(Properties.Resources._11heart);
            cardImages.Add(Properties.Resources._11spade);
            cardImages.Add(Properties.Resources._12diamond);
            cardImages.Add(Properties.Resources._12club);
            cardImages.Add(Properties.Resources._12heart);
            cardImages.Add(Properties.Resources._12spade);
            cardImages.Add(Properties.Resources._13diamond);
            cardImages.Add(Properties.Resources._13club);
            cardImages.Add(Properties.Resources._13heart);
            cardImages.Add(Properties.Resources._13spade);
            cardImages.Add(Properties.Resources.smalljoker);
            cardImages.Add(Properties.Resources.bigjoker);
        }
        public PokerCard(int num, int X, int Y)
        {
            this.Click += OnClick;
            number = num / 256 / 256;
            color = num - number * 256 * 256;
            if (color > 3)
            {
                if (color == 4)
                {
                    Image = cardImages[52];
                }
                else
                {
                    Image = cardImages[53];
                }
            }
            else
            {
                int id = (number - 1) * 4 + color;
                Image = cardImages[id];
            }
            Size = new Size(CARD_W, CARD_H);
            Location = new Point(X, Y);
            BackColor = System.Drawing.Color.Transparent;
            SizeMode = PictureBoxSizeMode.Zoom;
            BringToFront();
        }
        public static PokerCard FromInt(int num, int X, int Y)
        {
            return new PokerCard(num, X, Y);
        }
        public int ToInt()
        {
            return number * 256 * 256 + color;
        }
        public void OnClick(object sender, EventArgs e)
        {
            if (!bChosen)
            {
                bChosen = true;
                int y = Location.Y - DELTA_Y;
                Location = new Point(Location.X, y);
            }
            else
            {
                int y = Location.Y + DELTA_Y;
                Location = new Point(Location.X, y);
                bChosen = false;
            }
        }

        public void ResizeHeight(int cardHeight)
        {
            int cardWidth = (int)(cardHeight / RATIO);
            this.Size = new Size(cardWidth, cardHeight);
        }
    }
}
